// import com.alibaba.druid.sql.ast.SQLDataType;
// import com.alibaba.druid.sql.ast.SQLExpr;
// import com.alibaba.druid.sql.ast.SQLExprImpl;
// import com.alibaba.druid.sql.ast.SQLReplaceable;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.Collections;
// import java.util.List;

#include "SQLExtractExpr.h"
#include "SQLIntervalUnit.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../SQLExpr.h"
#include "../SQLDataType.h"
#include "../../../utils/instanceof.h"
#include "../SQLDataTypeImpl.h"
#include "SQLIntegerExpr.h"

SQLExtractExpr::SQLExtractExpr()
{
}

SQLObject_ptr SQLExtractExpr::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLExtractExpr>(new SQLExtractExpr());
  std::shared_ptr<SQLExtractExpr> x = std::dynamic_pointer_cast<SQLExtractExpr>(clone_tmp);
  // SQLExtractExpr x = new SQLExtractExpr();
  if (value != nullptr)
  {
    x->setValue(std::dynamic_pointer_cast<SQLExpr>(value->clone()));
  }
  x->unit_ = unit_;
  return x;
}

SQLExpr_ptr SQLExtractExpr::getValue()
{
  return value;
}

void SQLExtractExpr::setValue(SQLExpr_ptr value)
{
  if (value != nullptr)
  {
    value->setParent(SharedObject(SQLExtractExpr));
  }
  this->value = value;
}

std::shared_ptr<SQLIntervalUnit> SQLExtractExpr::getUnit()
{
  return unit_;
}

void SQLExtractExpr::setUnit(std::shared_ptr<SQLIntervalUnit> unit_)
{
  this->unit_ = unit_;
}

void SQLExtractExpr::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLExtractExpr)))
  {
    if (this->value != nullptr)
    {
      this->value->accept(visitor);
    }
  }
  visitor->endVisit(SharedObject(SQLExtractExpr));
}

//@Override
BOOL_ptr SQLExtractExpr::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (this->value == expr)
  {
    setValue(target);
    return BOOL::TRUE;
  }
  return BOOL::FALSE;
}

//@Override
SQLObject_list_ptr  SQLExtractExpr::getChildren()
{
  SQLObject_list_ptr  tmp = SQLObject_list_ptr ();
  tmp->push_back(value);
  return tmp;
}

//@Override
int SQLExtractExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  result = prime * result + ((unit_ == nullptr) ? 0 : unit_->hashCode());
  result = prime * result + ((value == nullptr) ? 0 : value->hashCode());
  return result;
}

//@Override
bool SQLExtractExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == nullptr)
  {
    return false;
  }
  if (!(instanceof <Object, SQLExtractExpr>(obj.get())))
  {
    return false;
  }
  std::shared_ptr<SQLExtractExpr> other = std::dynamic_pointer_cast<SQLExtractExpr>(obj);
  if (unit_ != other->unit_)
  {
    return false;
  }
  if (value == nullptr)
  {
    if (other->value != nullptr)
    {
      return false;
    }
  }
  else if (!value->equals(other->value))
  {
    return false;
  }
  return true;
}

SQLDataType_ptr SQLExtractExpr::computeDataType()
{
  return SQLIntegerExpr::DATA_TYPE;
}
